﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OreSystem.Entities;
using NHibernate;
namespace OreSystem.DAL {
    public class UserDAL {
        
        public User GetUserByUserName(string userName) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                IList<User> userList = session.CreateQuery("from User u where u.UserName =:name").SetString("name", userName).List<User>();
                if (userList.Count > 0) {
                    return userList.First();
                }
                return null;
            }
        }

        public IList<User> GetUserByKeys(string keys) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                return session.CreateQuery("from User u where u.UserName like:key").SetString("key","%"+keys+"%").List<User>();
            }
        }

        public void Save(User u) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                session.SaveOrUpdate(u);
                session.Flush();
            }
        }
        public void Delete(User u) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                session.Delete(u);
                session.Flush();
            }
        }
        public User GetUserById(int id) {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                return session.Get<User>(id);
            }
        }

        public IList<User> GetAllList() {
            using (ISession session = DaoFactory.CreateSessionFactory().OpenSession()) {
                return session.CreateQuery("from User").List<User>();
            }
        }
    }
}
